<header class="header navbar bg-white shadow">
  <div class="btn-group tool-button">
    <a class="btn btn-primary navbar-btn" href="/private/wx/menu/index/<%- wxid %>" data-pjax><i
        class="ti-angle-left"></i> 返回</a>
  </div>
</header>
<div class="content-wrap">
  <div class="wrapper" style="min-height:500px;">
    <section class="panel">
      <form id="addForm" role="form" class="form-horizontal parsley-form" data-parsley-validate
            action="/private/wx/menu/addDo" method="post">
        <input id="wxid" name="wxid" type="hidden" value="<%- wxid %>">

        <div class="row mb10">
          <div class="col-lg-12">
            <div class="form-group">
              <label for="parentId" class="col-sm-2 control-label">上级菜单</label>

              <div class="col-sm-8">
                <select id="parentId" name="parentId" class="form-control">
                  <option value="0">无</option>
                  <% if(menus){
                    menus.forEach(function(o){
                  %>
                  <option value="<%= o.id %>"><%- o.name %></option>
                  <%
                  });
                  } %>
                </select>
                <input id="errMsg" value="false" type="hidden">
                <p class="help-block no-m" style="color: red;" id="_errMsg"></p>
              </div>
            </div>
            <div class="form-group">
              <label for="name" class="col-sm-2 control-label">菜单名称</label>

              <div class="col-sm-8">
                <input id="name" name="name" type="text" class="form-control"
                       data-parsley-maxlength="20" data-parsley-required="true">

                <p class="help-block no-m">一级菜单最多4个汉字，二级菜单最多7个汉字，多出来的部分将会以“...”代替。</p>
              </div>
            </div>
            <div class="form-group">
              <label for="type" class="col-sm-2 control-label">菜单类型</label>

              <div class="col-sm-8 ">
                <div class="radio"><label><input onclick="clickRadio('')" type="radio" name="type" value="" checked>无</label></div>
                <div class="radio"><label><input onclick="clickRadio('view')" type="radio" name="type" value="view">链接</label></div>
                <div class="radio"><label><input onclick="clickRadio('click')" type="radio" name="type" value="click">事件</label></div>
              </div>
            </div>
            <div id="div_url" class="form-group" style="display: none">
              <label for="url" class="col-sm-2 control-label">URL</label>

              <div class="col-sm-8">
                <input id="url" name="url" type="text" class="form-control">
                <input type="checkbox" id="btnUrl">网页oauth2.0
                <input type="checkbox" id="btnUrl2">商城oauth2.0

              </div>
            </div>
            <div id="div_key" class="form-group" style="display: none">
              <label for="key" class="col-sm-2 control-label">事件Key</label>

              <div class="col-sm-8">
                <div class="input-group">
                  <input id="key" name="key" type="text" class="form-control"
                         data-parsley-maxlength="20">
                  <span class="input-group-btn">
                  <button class="btn btn-color current-time" type="button" onclick="selKeyword()">选择关键词</button>
                  </span>
                </div>
              </div>
            </div>


          </div>

        </div>
        <div class="col-lg-3"></div>
        <div class="col-lg-6">
          <div class="form-group text-center">
            <label></label>

            <div>
              <button class="btn btn-primary btn-block btn-lg btn-parsley" data-loading-text="正在提交..." id="submit">提 交
              </button>
            </div>
          </div>
        </div>
      </form>
    </section>

  </div>
</div>
<div id="dialogKeySelect" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">

    </div>
  </div>
</div>
<script language="JavaScript">
  var msgtype;
  function clickRadio(type){
    if (type == 'click') {
      $("#div_url").hide();
      $("#div_key").show();
      $("#url").val("");
    } else if (type == 'view') {
      $("#div_key").hide();
      $("#div_url").show();
      $("#key").val("");
    } else {
      $("#div_key").hide();
      $("#div_url").hide();
      $("#key").val("");
      $("#url").val("");
    }
  }
  function selKeyword(){
    $("#dialogKeySelect").modal({
      remote: "/private/wx/menu/keyword/<%=wxid%>"
    });
  }
  function checkDo(){
    $.post("/private/wx/menu/checkDo", {wxid:'<%=wxid%>',parentId:$("#parentId").val()}, function (data) {
      if (data.code == 0) {
        $("#errMsg").val("false");
      } else {
        $("#errMsg").val("true");
      }
      $("#_errMsg").html(data.msg);

    }, "json");

  }
  function getUrl(url){
    var str="https://open.weixin.qq.com/connect/oauth2/authorize?appid=<%=config.appid%>&redirect_uri=$s&response_type=code&scope=snsapi_base&state=11624317#wechat_redirect";
    return str.replace("$s",encodeURIComponent(url));
  }
  function seeUrl(url){
    var str=url.substring(url.indexOf("redirect_uri=")+13,url.indexOf("&response_type="));
    return decodeURIComponent(str);
  }
  function getUrl2(url){
    var str="http://<%=sails.config.system.AppDomain%>/wap/wechat/oauth?goto_url=$s";
    return str.replace("$s",url);
  }
  function seeUrl2(url){
    return url.substring(url.indexOf("goto_url=")+9);
  }
  $(document).ready(function () {
    $("#btnUrl").on("click",function(){
      if($(this).is(':checked')){
        $("#url").val(getUrl($("#url").val()));
      }else{
        $("#url").val(seeUrl($("#url").val()));
      }
    });
    $("#btnUrl2").on("click",function(){
      if($(this).is(':checked')){
        $("#url").val(getUrl2($("#url").val()));
      }else{
        $("#url").val(seeUrl2($("#url").val()));
      }
    });
    checkDo();
    $("#parentId").on("change",function(){
      checkDo();
    });
    $("#dialogKeySelect").on("hidden.bs.modal", function () {
      $(this).removeData("bs.modal");
    });
    $('#addForm').ajaxForm({
      dataType: 'json',
      beforeSubmit: function (arr, form, options) {
        if($("#errMsg").val()=='true')
        {
          return false;
        }
      },
      success: function (data, statusText, xhr, form) {
        if (data.code == 0) {
          Toast.success(data.msg);
          form.resetForm();checkDo();
        } else {
          Toast.error(data.msg);
        }
        form.find("button:submit").button("reset");
      }
    });
  });
</script>
